﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Encryption.Models
{
    /// <summary>
    /// 加密类
    /// </summary>
    internal class MyEncoder
    {
        /// <summary>
        /// 明文
        /// </summary>
        public string Plain { get; set; }

        /// <summary>
        /// 密钥
        /// </summary>
        public string Key { get; set; }

        /// <summary>
        /// 密文
        /// </summary>
        public string Cypher { get; set; }

        /// <summary>
        /// 加密动作
        /// </summary>
        public void Encode()
        {
            //加密过程
            for (int i = 0, j = 0; i < Plain.Length; i++, j++)
            {
                //遍历密钥中的所有字母
                if (j % Key.Length == 0)
                    j = 0;
                int a = (ToNum(Plain[i]) + ToNum(Key[j])) % 26;
                Cypher += Convert.ToChar(a + 'a');
            }
        }

        /// <summary>
        /// 返回指定字符在字母表中的序号（从0开始）
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        private int ToNum(char ch)
        {
            return ch - 'a';
        }
    }
}
